package com.cn.web.controller.PartsManagement;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.vo.PartsManagement.BusinessDepartmentType;
import com.cn.bean.vo.PartsManagement.EquipmentBasicInfo;
import com.cn.bean.vo.bc.UserDepartments;
import com.cn.config.config.Result;
import com.cn.service.server.PartsManagement.ComponentItemClassificationService;
import com.cn.service.server.PartsManagement.EquipmentBasicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Eq")
@Transactional
public class EquipmentBasicInfoController {
    private final EquipmentBasicInfoService equipmentBasicInfoService;

    @Autowired(required = false)
    public EquipmentBasicInfoController(EquipmentBasicInfoService equipmentBasicInfoService) {
        this.equipmentBasicInfoService = equipmentBasicInfoService;
    }

    @RequestMapping("/select/EquipmentBasicInfo")
    @ResponseBody
    public List<EquipmentBasicInfo> selectUserDepartments(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String equipmentCode = jsonObject.getString("equipmentCode");
        String equipmentName = jsonObject.getString("equipmentName");
        equipmentCode = (equipmentCode != null) ? equipmentCode : "";
        equipmentName = (equipmentName != null) ? equipmentName : "";
        return equipmentBasicInfoService.selectEquipmentBasicInfo(limit, page, equipmentCode, equipmentName);
    }

    @RequestMapping("/add/EquipmentBasicInfo")
    @ResponseBody
    public Result addEquipmentBasicInfo(@RequestBody JSONObject jsonObject) {
//        Integer ID = jsonObject.getInteger("ID");
//        String equipmentCode = jsonObject.getString("equipmentCode");
        String equipmentName = jsonObject.getString("equipmentName");
        Integer responsiblePerson = jsonObject.getInteger("responsiblePerson");
        Integer custodian = jsonObject.getInteger("custodian");
        Integer user = jsonObject.getInteger("user");
        String specificationModel = jsonObject.getString("specificationModel");
        String supplier = jsonObject.getString("supplier");
        String description = jsonObject.getString("description");
        String storageLocation = jsonObject.getString("storageLocation");
        Integer equipmentStatus = jsonObject.getInteger("equipmentStatus");
        Integer structureId=jsonObject.getInteger("structureId");
        Integer equipmentType=jsonObject.getInteger("equipmentType");
        Integer procedures=jsonObject.getInteger("procedures");
        return equipmentBasicInfoService.addEquipmentBasicInfo(structureId,equipmentName, responsiblePerson, custodian, user, specificationModel, supplier, description, storageLocation, equipmentStatus,equipmentType,procedures);
    }

    @RequestMapping("/delete/EquipmentBasicInfo")
    @ResponseBody
    public Result deleteEquipmentBasicInfo(@RequestBody JSONObject jsonObject) {
        JSONArray deleteIdList = jsonObject.getJSONArray("deleteIdList");
        List<EquipmentBasicInfo> idList = new ArrayList<>();
        for (int i = 0; i < deleteIdList.size(); i++) {
            JSONObject deleteIdObject = deleteIdList.getJSONObject(i);
            int id = deleteIdObject.getInteger("ID");
            EquipmentBasicInfo personnel = new EquipmentBasicInfo();
            personnel.setID(id);
            idList.add(personnel);
        }

        return equipmentBasicInfoService.deleteEquipmentBasicInfo(idList);
    }

    @RequestMapping("/upDate/EquipmentBasicInfo")
    @ResponseBody
    public Result upDateEquipmentBasicInfo(@RequestBody JSONObject jsonObject) {
        Integer ID = jsonObject.getInteger("ID");
//        String equipmentCode = jsonObject.getString("equipmentCode");
        String equipmentName = jsonObject.getString("equipmentName");
        Integer responsiblePerson = jsonObject.getInteger("responsiblePerson");
        Integer custodian = jsonObject.getInteger("custodian");
        Integer user = jsonObject.getInteger("user");
        String specificationModel = jsonObject.getString("specificationModel");
        String supplier = jsonObject.getString("supplier");
        String description = jsonObject.getString("description");
        String storageLocation = jsonObject.getString("storageLocation");
        Integer equipmentStatus = jsonObject.getInteger("equipmentStatus");
        Integer structureId=jsonObject.getInteger("structureId");
        Integer equipmentType=jsonObject.getInteger("equipmentType");
        Integer procedures=jsonObject.getInteger("procedures");
        return equipmentBasicInfoService.upDateEquipmentBasicInfo(structureId,ID, equipmentName, responsiblePerson, custodian, user, specificationModel, supplier, description, storageLocation, equipmentStatus,equipmentType,procedures);
    }
    @RequestMapping("/select/EquipmentBasicInfo/Auto1")
    @ResponseBody
    public List<EquipmentBasicInfo> selectEquipmentBasicInfoAuto1(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String equipmentCode = jsonObject.getString("input1");
        String equipmentName = jsonObject.getString("input2");
        equipmentCode = (equipmentCode != null) ? equipmentCode : "";
        equipmentName = (equipmentName != null) ? equipmentName : "";
        return equipmentBasicInfoService.selectEquipmentBasicInfo(limit, page, equipmentCode, equipmentName);
    }


}
